package com.action.gulimall.search.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.HttpAsyncResponseConsumerFactory;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * @ClassName RestClientConfig
 * @Deacription
 * @Author sl
 * @Date 2021/6/6/006 18:00
 * @Version 1.0
 **/
@Configuration
public class RestClientConfig {
    public static final RequestOptions COMMON_OPTIONS;
    static {
        RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
        //builder.addHeader("Authorization", "Bearer " + TOKEN);
//        builder.setHttpAsyncResponseConsumerFactory(
//                new HttpAsyncResponseConsumerFactory
//                        .HeapBufferedResponseConsumerFactory(30 * 1024 * 1024 * 1024));
        COMMON_OPTIONS = builder.build();
    }

//    @Value(value = "${elasticsearch.host}")
//    private static String elasticsearch_host;
//
//    @Value(value = "${elasticsearch.port}")
//    private static String elasticsearch_port;
//    static {
//        restClient = new RestHighLevelClient(
//                RestClient.builder(
//                        new HttpHost(elasticsearch_host, Integer.valueOf(elasticsearch_port), "http")
//                        /*,new HttpHost("localhost", 9201, "http")*/
//                )
//        );
//        System.out.println("restClient init in service.");
//    }

    @Bean
    public RestHighLevelClient getRestClient(@Value(value = "${elasticsearch.host}") String elasticsearch_host,@Value(value = "${elasticsearch.port}") String elasticsearch_port){
        return new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost(elasticsearch_host, Integer.valueOf(elasticsearch_port), "http")
                        /*,new HttpHost("localhost", 9201, "http")*/
                )
        );
    }

}
